$(function () {
    init();
});

/**
 * @Author: Mr.Yan
 * @date: 2019/1/11
 * @Description: 初始化
 */
function init() {
    layui.use(['layer', 'vip_nav'], function () {
        // 操作对象
        var layer = layui.layer
            , vipNav = layui.vip_nav
            , $ = layui.jquery;
        layTool.toplayer = layer;
    });
}


/**
 * @Author: Mr.Yan
 * @date: 2019/1/11
 * @Description: 修改密码
 */
function changePass(userId, passWord) {
    var title = "修改密码",
        url = prefixOpen + "/index!changePass@userId=" + userId,
        width = "30%",
        height = "40%";
    //  打开新窗口
    layTool.openDialog(title, url, width, height, changePassOk);
}

/**
 * @Author: Mr.Yan
 * @date: 2019/1/11
 * @Description: 修改密码确定回调
 */
function changePassOk(index, layero) {
    // 获取子类窗口的form 表单
    var body = layTool.getChildFrame("body", index);
    var userId = body.find('#userId').val();
    var oldPassWord = body.find('#oldPassWord').val();
    var newPassWord = body.find('#newPassWord').val();
    var newPassWordIsOk = body.find('#newPassWordIsOk').val();
    var form = $(body[0]).find('#form');
    if (validate_form(form[0])) {
        // 验证旧密码是否相同
        // 发送保存地址
        $.ajax({
            url: prefixUser + "/changePass",
            data: {
                "userId" :userId,
                "oldPassWord" :oldPassWord,
                "newPassWord" :newPassWord,
                "newPassWordIsOk" :newPassWordIsOk,
            },
            type: "POST",
            beforeSend: function () {
                loading = layTool.toplayer.load(0, {shade: false});
            },
            success: function (data) {
                layTool.toplayer.close(loading);
                var errcode = data.errcode;
                var errmsg = data.errmsg;
                if (errcode == "0") {
                    layTool.closeLayer(index);
                    layTool.showSucAlert(errmsg);
                    setTimeout(function () {
                        // 2s 重新登录
                        window.location.href = ctx + "logout";
                    }, 2000);
                } else {
                    layTool.showAlert(errmsg);
                }
            },
            error: function (e) {
                layTool.toplayer.close(loading);
                AjaxErro();
            }
        })
    }
}
